﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataMining.Bean;
using System.Data.SqlClient;
using DataMining.DataAccess.HospitalDataSetTableAdapters;
using System.Data;
using System.Windows;

namespace DataMining.DataAccess
{
    class LIS_Allergy_ResultAccess
    {
        public void InsertList(List<LIS_Allergy_Result> resultlist)
        {
            //通过单例模式获取系统唯一的connection
            MyConnection myconn = new MyConnection();
            SqlConnection connection = myconn.getSqlConnection2();
            connection.Open();

            //设置command
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select * from LIS_Allergy_Result";
            //定义Adapter
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
            adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
            adapter.InsertCommand = commandBuilder.GetInsertCommand();
            adapter.DeleteCommand = commandBuilder.GetDeleteCommand();

            //定义数据Table
            DataTable resultTable = new DataTable("LIS_Allergy_Result");
            adapter.Fill(resultTable);//装载数据表
            //int count = 0;
            //设置TDS的数据库每次提交的数量
           // int batchsiza = 10;
            //adapter.UpdateBatchSize = batchsiza;
            foreach (LIS_Allergy_Result result in resultlist)
            {
                //count++;
                DataRow row = resultTable.NewRow();
                //通过反射机制填入row中
                RowValueFromDataTable.GetValueFromTable(result, row);
                resultTable.Rows.Add(row);
                //
                //if (count % batchsiza == 0)
                //{
                    try
                    { 
                        adapter.Update(resultTable);
                    }
                    catch (SqlException ex) 
                    {
                        if (ex.Number != 2627)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    finally 
                    {
                        resultTable.Clear();
                    }
                    
                    
                //}
            }
            adapter.Update(resultTable);
            resultTable.Clear();
            connection.Close();
            //return count;
        }
    }
}
